package com.xwqkwl.aimin.auth.service;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.shaded.com.google.common.base.Joiner;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.xwqkwl.aimin.auth.constants.WxApiUrlconstants;
import com.xwqkwl.aimin.auth.pojo.jscode2sessionResult;
import com.xwqkwl.aimin.auth.properties.WXproperties;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class WxService {

    private final RestTemplate restTemplate;
    private final WXproperties wxproperties;

    public jscode2sessionResult WxLogin(String code){

        Map<String,String> params = new HashMap<>(6);
        params.put("appid",wxproperties.getAppid());
        params.put("secret",wxproperties.getSecret());
        params.put("js_code",code);
        params.put("grant_type","authorization_code");

        String paramsStr = Joiner.on("&").withKeyValueSeparator("=").join(params);
        String url = WxApiUrlconstants.WX_API_URL + "?" + paramsStr;
        String response = restTemplate.getForObject(url, String.class);
        return JSONObject.parseObject(response,jscode2sessionResult.class);
    }
}
